import moment from "moment";
import { useEffect, useState, useRef } from "react";
function useTime() {
    const [time, setTime] = useState(moment().format("YYYY-MM-DD HH:mm:ss"));
    const timer = useRef(null);
    useEffect(() => {
        timer.current = setInterval(() => {
            setTime(moment().format("YYYY-MM-DD HH:mm:ss"));
            console.log("load");
        }, 1000);
        return () => {
            console.log("clear");
            clearInterval(timer.current);
        };
    }, [time]);

    const clear = () => {
        if (timer.current) clearInterval(timer.current);
    };

    const reset = () => {
        setTime(moment().format("YYYY-MM-DD HH:mm:ss"));
    };
    return { time, clear, reset };
}
function App() {
    const { time, clear, reset } = useTime();
    return (
        <div>
            {time}
            <br />
            <button
                onClick={() => {
                    clear();
                }}>
                暂停时间
            </button>
            <button
                onClick={() => {
                    reset();
                }}>
                继续播放
            </button>
        </div>
    );
}
export default App;
